package com.daidai.transform;

import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.util.Arrays;

public class SideOutputs {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<Integer> source = env.fromCollection(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));

        //数据分流，奇数，偶数分流

        //定义tag
        OutputTag<Integer> tag0 = new OutputTag<>("偶数", TypeInformation.of(Integer.class));
        OutputTag<Integer> tag1 = new OutputTag<>("奇数", TypeInformation.of(Integer.class));

        //给数据打上tag
        SingleOutputStreamOperator<Integer> integerSingleOutputStreamOperator = source.process(new ProcessFunction<Integer, Integer>() {
            @Override
            public void processElement(Integer value, ProcessFunction<Integer, Integer>.Context ctx, Collector<Integer> out) throws Exception {
                if (value % 2 == 0) {
                    ctx.output(tag0, value);
                } else {
                    ctx.output(tag1, value);
                }
            }
        });

        //从有tag的数据中取数据
        DataStream<Integer> sideOutput0 = integerSingleOutputStreamOperator.getSideOutput(tag0);
        DataStream<Integer> sideOutput1 = integerSingleOutputStreamOperator.getSideOutput(tag1);

        //print
        sideOutput0.print("偶数");
        sideOutput1.print("奇数");


        env.execute();

    }
}
